package cn.iocoder.yudao.module.cmt.service.venue;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.venue.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.venue.VenueBookingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.venue.VenueBookingMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class VenueBookingServiceImpl implements VenueBookingService {

    @Resource
    private VenueBookingMapper venueBookingMapper;

    @Override
    public Long createVenueBooking(VenueBookingSaveReqVO createReqVO) {
        VenueBookingDO venueBooking = BeanUtils.toBean(createReqVO, VenueBookingDO.class);
        venueBookingMapper.insert(venueBooking);
        return venueBooking.getId();
    }

    @Override
    public void updateVenueBooking(VenueBookingSaveReqVO updateReqVO) {
        validateVenueBookingExists(updateReqVO.getId());
        VenueBookingDO updateObj = BeanUtils.toBean(updateReqVO, VenueBookingDO.class);
        venueBookingMapper.updateById(updateObj);
    }

    @Override
    public void deleteVenueBooking(Long id) {
        validateVenueBookingExists(id);
        venueBookingMapper.deleteById(id);
    }

    private void validateVenueBookingExists(Long id) {
        if (venueBookingMapper.selectById(id) == null) {
            throw exception(VENUE_BOOKING_NOT_EXISTS);
        }
    }

    @Override
    public VenueBookingDO getVenueBooking(Long id) {
        return venueBookingMapper.selectById(id);
    }

    @Override
    public PageResult<VenueBookingDO> getVenueBookingPage(VenueBookingPageReqVO pageReqVO) {
        return venueBookingMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVenueBookingListByIds(List<Long> ids) {
        ids.forEach(this::validateVenueBookingExists);
        venueBookingMapper.deleteBatchIds(ids);
    }

}
